{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import csv\n",
    "import json\n",
    "\n",
    "symbol = {',', '\\'', '.', '。', ']', '[', ';', ':', '!', '?'}\n",
    "\n",
    "def readCsv(fileName: str):\n",
    "    word = set()\n",
    "    with open(fileName, 'r',encoding='utf-8') as f:\n",
    "        reader = csv.reader(f)\n",
    "        head = True\n",
    "        for row in reader:\n",
    "            if (head):\n",
    "                head = False\n",
    "                continue\n",
    "            lt = json.loads(row[4].replace(\"\\'\", '\"'))\n",
    "            for i in lt:\n",
    "                for j in i:\n",
    "                    if j not in symbol:\n",
    "                        word.add(j)\n",
    "    return word\n",
    "\n",
    "\n",
    "def writeWordInTxt(fileName: str,list):\n",
    "    with open(fileName, \"w\", newline=\"\\n\") as writer:\n",
    "        for e in list:\n",
    "            writer.write(e+'\\n')\n",
    "\n",
    "def readWordListFromTxt(fileName: str):\n",
    "    result = set()\n",
    "    with open(fileName,'r') as f:\n",
    "        all = f.readlines()\n",
    "        for e in all:\n",
    "            list = json.loads(e.replace(\"\\'\", '\"'))\n",
    "            for i in list:\n",
    "                for j in i:\n",
    "                    if j not in symbol:\n",
    "                        result.add(j)\n",
    "    return result\n",
    "\n",
    "words = readWordListFromTxt('fenci.txt')\n",
    "writeWordInTxt('words.txt',list(words))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
